#include "lircthread.h"
#include <lirc/lirc_client.h>
#include <QStringList>
//#include <QStringStream>
#include <QtCore/QDebug>
lircThread::lircThread():QThread()
{
	cont=1;
}
void lircThread::run()
{
	qDebug("exec");
	lirc_init("qlirc",1);
	lirc_readconfig(NULL,&config,NULL);
	char*code;
	keyData tmp;
	while (cont)
	{
		lirc_nextcode(&code);
//		std::cout <<"got code: "<<code<<std::endl;
		tmp=parseKey(code);
//		qDebug()<<tmp.str();
		if(tmp.seqNo)
			emit keyRepeated(tmp);
		else
			emit keyPressed(tmp);
	}
	return ;
}
keyData lircThread::parseKey(char * code)
{
	QString str(code);
	str.remove('\n');
	keyData ret;
	QStringList parts=str.split(' ');
	ret.code=parts[0].toULong(0,16);
	ret.seqNo=parts[1].toInt(0,16);
	ret.name=parts[2];
	ret.device=parts[3];


	return ret;
}
QString keyData::str()
{
	return QString("key(%1, %2, %3, %4)").arg(code,0,16).arg(seqNo).arg(name).arg(device);
}
//lircThread::~lircThread():~QThread()
//{
//
//}
